<template>
  <div class="app-container">
    <el-col :span="1.5">
      <el-checkbox
        true-label="0"
        false-label="1"
        v-model="isAll"
        @change="changeisAll"
        >全选</el-checkbox
      >
    </el-col>
    <el-table
      ref="dragTable"
      :data="columns"
      row-key="id"
      :max-height="tableHeight"
    >
      <el-table-column
        label="序号"
        type="index"
        min-width="50%"
        class-name="allowDrag"
      />
      <el-table-column
        label="字段描述"
        min-width="10%"
        prop="comment"
        class-name="allowDrag"
      />
      <el-table-column
        label="字段列名"
        prop="field_name"
        min-width="10%"
        :show-overflow-tooltip="true"
        class-name="allowDrag"
      />
      <el-table-column label="显示状态" min-width="5%">
        <template slot-scope="scope">
          <el-checkbox
            true-label="1"
            false-label="0"
            v-model="scope.row.status"
            @change="handleCheckboxChange(scope.row)"
          ></el-checkbox>
        </template>
      </el-table-column>
    </el-table>
    <el-form label-width="100px">
      <el-form-item
        style="text-align: center; margin-left: -100px; margin-top: 10px"
      >
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template> 
<script>
import axios from "axios";
import { deleteoperation, bitableoperation } from "@/api/conf/bitableoperation";
import Sortable from "sortablejs";
//--------导入token
import { getToken } from "@/utils/auth";
import {
  listBiPropertyLocation,
  selectProperties,
  updatePropertyStatus,
} from "@/api/conf/BiPropertyLocation";
export default {
  props: {
    tableName: {
      type: String,
      default: "",
    },
    countCols: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      isAll: "1",
      // 选中选项卡的 name
      activeName: "columnInfo",
      // 表格的高度
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      // 表信息
      tables: [],
      // 表列信息
      columns: [],
      // 字典信息
      dictOptions: [],
      // 菜单信息
      menus: [],
      // 表详细信息
      info: {},
    };
  },
  watch: {
    countCols: {
      handler: function (newnew, oldold) {
        this.getList();

        // this.getselectProductSaleRatioTop();
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    this.getList();
  },
  methods: {
    changeisAll() {
      if (this.isAll == "0") {
        this.columns.forEach((item) => {
          item.status = "1";
          // item.allocationMode = "0";
          this.handleCheckboxChange(item);
        });
      } else {
        this.columns.forEach((item) => {
          item.status = "0";
          this.handleCheckboxChange(item);
        });
      }
    },
    handleCheckboxChange(row) {
      console.log(JSON.stringify(row));
      let obj = {
        tableName: this.tableName,
        status: row.status,
        propertyArr: row.fieldName,
      };
      updatePropertyStatus(obj).then((response) => {});
    },
    getList() {
      let obj = {
        tableName: this.tableName,
        pageNum: 1,
        pageSize: 100,
      };
      listBiPropertyLocation(obj).then((response) => {
        response.rows.forEach((item) => {
          item.comment = item.fieldComment;
          item.field_name = item.fieldName;
        });
        this.columns = response.rows;
      });
      // response.rows.forEach((item) => {
      //   item
      //      });
      //   this.columns = response.data.cols ? response.data.cols : [];
      //   this.columns.forEach((item, index) => {
      //     item.columnId = index.toString();

      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.handleadjustFieldPosition();
    },
    handleadjustFieldPosition() {
      let fd = [...this.columns];
      // fd.tableTotal = this.cols.map((item) => {
      //   item.field_name, item.comment;
      // });
      axios
        .put(
          process.env.VUE_APP_BASE_API +
            "/conf/bitableoperation/adjustFieldPosition?tableName=" +
            this.tableName,
          fd,
          {
            headers: {
              header: {
                // responseType: "blob"
              },

              Authorization: "Bearer " + getToken(),
              // "Content-Type": "multipart/form-data",
            }, //定义内容格式,很重要
            // timeout: 20000,
          }
        )
        .then((response) => {
          if (response.data.code == 200) {
            this.$modal.msgSuccess("调整成功");
          } else {
            this.$modal.msgError("调整失败");
          }
        });
    },
    /** 关闭按钮 */
    close() {
      this.$emit("close");
    },
  },
  mounted() {
    const el = this.$refs.dragTable.$el.querySelectorAll(
      ".el-table__body-wrapper > table > tbody"
    )[0];
    const sortable = Sortable.create(el, {
      handle: ".allowDrag",
      onEnd: (evt) => {
        const targetRow = this.columns.splice(evt.oldIndex, 1)[0];
        this.columns.splice(evt.newIndex, 0, targetRow);
        for (let index in this.columns) {
          this.columns[index].sort = parseInt(index) + 1;
        }
      },
    });
    console.log(JSON.stringify(this.columns));
  },
};
</script>